//
//  ImageAnimationView.swift
//  AICleaner
//
//  Created by firefly on 2025/1/22.
//

import SwiftUI

struct ImageAnimationView: View {
    // 图片名称数组
    let imageNames = ["ai_clean_01", "ai_clean_02", "ai_clean_03", "ai_clean_04", "ai_clean_05", "ai_clean_06", "ai_clean_07", "ai_clean_08", "ai_clean_09", "ai_clean_10", "ai_clean_11", "ai_clean_12"]
    
    // 当前显示的图片索引
    @State private var currentIndex = 0
    
    // 定时器，用于自动切换图片
    let timer = Timer.publish(every: 0.5, on: .main, in: .common).autoconnect()
    
    var body: some View {
        VStack {
            // 显示当前图片
            Image(imageNames[currentIndex])
                .resizable()
                .scaledToFit()
                .transition(.opacity) // 使用淡入淡出效果
                .id(currentIndex) // 强制视图更新
            
//            // 手动切换按钮
//            Button("Next Image") {
//                withAnimation(.easeInOut(duration: 0.5)) {
//                    currentIndex = (currentIndex + 1) % imageNames.count
//                }
//            }
//            .padding()
        }
        .onReceive(timer) { _ in
            withAnimation(.easeInOut(duration: 0.5)) {
                currentIndex = (currentIndex + 1) % imageNames.count
            }
        }
    }
}

struct ImageAnimationView_Previews: PreviewProvider {
    static var previews: some View {
        ImageAnimationView()
    }
}
